<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Menu extends Model
{
    use HasFactory;

    protected $fillable = [
        'name',
        'description',
        'room_id',
        'creator_id',
        'source_type',
        'source_id',
        'is_public',
        'is_template',
        'usage_count',
        'rating',
    ];

    protected $casts = [
        'is_public' => 'boolean',
        'is_template' => 'boolean',
        'rating' => 'decimal:2',
    ];

    /**
     * 准备日期用于数组/JSON序列化
     */
    protected function serializeDate(\DateTimeInterface $date): string
    {
        return $date->format('Y-m-d H:i:s');
    }

    /**
     * 关联房间
     */
    public function room(): BelongsTo
    {
        return $this->belongsTo(Room::class);
    }

    /**
     * 菜单创建者
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'creator_id');
    }

    /**
     * 来源菜单
     */
    public function sourceMenu(): BelongsTo
    {
        return $this->belongsTo(Menu::class, 'source_id');
    }

    /**
     * 菜单菜品
     */
    public function dishes(): HasMany
    {
        return $this->hasMany(Dish::class);
    }

    /**
     * 使用此菜单的房间
     */
    public function rooms(): HasMany
    {
        return $this->hasMany(Room::class);
    }

    /**
     * 获取菜品数量
     */
    public function getDishCountAttribute(): int
    {
        return $this->dishes()->count();
    }

    /**
     * 增加使用次数
     */
    public function incrementUsageCount(): void
    {
        $this->increment('usage_count');
    }

    /**
     * 更新评分
     */
    public function updateRating(float $rating): void
    {
        $this->update(['rating' => $rating]);
    }

    /**
     * 作用域：公开菜单
     */
    public function scopePublic($query)
    {
        return $query->where('is_public', true);
    }

    /**
     * 作用域：模板菜单
     */
    public function scopeTemplate($query)
    {
        return $query->where('is_template', true);
    }

    /**
     * 作用域：用户菜单
     */
    public function scopeByCreator($query, int $creatorId)
    {
        return $query->where('creator_id', $creatorId);
    }

    /**
     * 作用域：按来源类型
     */
    public function scopeBySource($query, string $sourceType, int $sourceId = null)
    {
        $query = $query->where('source_type', $sourceType);
        if ($sourceId !== null) {
            $query->where('source_id', $sourceId);
        }
        return $query;
    }

    /**
     * 作用域：按评分排序
     */
    public function scopeByRating($query, string $direction = 'desc')
    {
        return $query->orderBy('rating', $direction);
    }

    /**
     * 作用域：按使用次数排序
     */
    public function scopeByUsage($query, string $direction = 'desc')
    {
        return $query->orderBy('usage_count', $direction);
    }

    /**
     * 搜索菜单
     */
    public function scopeSearch($query, string $keyword)
    {
        return $query->where(function ($q) use ($keyword) {
            $q->where('name', 'like', "%{$keyword}%")
              ->orWhere('description', 'like', "%{$keyword}%");
        });
    }

    /**
     * 检查菜单是否可用
     */
    public function isAvailable(): bool
    {
        return $this->is_public || $this->is_template;
    }
}